package com.beamfield.controller.manage.portal;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Carousel;
import com.beamfield.model.NewsSection;
import com.beamfield.modelVo.CarouselVo;
import com.beamfield.service.manage.portal.CarouseService;
import com.beamfield.service.manage.portal.NewsSectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 后台门户轮播图
 */
@RequestMapping("/cms/portal/carousel")
@Controller
public class CarouseController extends BaseController {

    private final CarouseService carouseService;

    @Autowired
    public CarouseController(CarouseService carouseService) {
        this.carouseService = carouseService;
    }

    @RequestMapping(value = "/carousel",method = RequestMethod.GET)
    public ModelAndView newsSectionPage(){
        return new ModelAndView();
    }

    /**
     * 获取轮播图
     * @param request
     * @return
     */
    @RequestMapping(value = "/readCarouseData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readCarouseData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        List<CarouselVo> carouselList = carouseService.getCarouseData(offset, limit, beamId);
        long total = carouseService.countCarouseData(beamId);
        return new PageResult(total, CommonUtils.parseLong(offset,0L), carouselList);
    }

    /**
     * 保存轮播图
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request,Carousel carousel){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        carousel.setBeam_id(beamId);
        carousel.setOperator(userId);
        carousel.setDel_flag(0);
        carousel.setAddtime(new Date());
        int result = 0;
        try{
            result = carouseService.save(carousel);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"新增成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 删除一条轮播图
     * @param request
     * @return
     */
    @RequestMapping(value = "/doDelete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelele(HttpServletRequest request){
        Long userId = super.getUserId(request);
        String carouselId = request.getParameter("carousel_id");
        if(StringUtils.isEmpty(carouselId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            result = carouseService.deleteOne(carouselId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 更新轮播图
     * @param request
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult update(HttpServletRequest request,Carousel carousel){
        Long userId = super.getUserId(request);
        Long carouselId = carousel.getCarousel_id();
        if(carouselId == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        carousel.setUpdater(userId);
        carousel.setUpdatetime(new Date());
        int result = 0;
        try{
            result = carouseService.updateOne(carousel);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"更新成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
}

